package murcielago.phonema;

import java.util.Vector;

/**
 * Only Support OR specification now and SPLIT function
 * @author Diego Sarmentero
 */
public class RegEx {

    private String pattern;

    public RegEx(String pattern){
        this.pattern = pattern;

    }

    public String[] split(String text){
        Vector data = new Vector();
        text = text.toLowerCase().trim();
        char[] exp = this.pattern.toCharArray();
        for(int i = 0; i < exp.length; i++){
            text = text.replace(exp[i], ' ');
        }

        int indexL = 0;
        int indexR = 0;
        while((indexR = text.indexOf(" ", indexR)) != -1){
            if(indexL != indexR){
                data.addElement(text.substring(indexL, indexR++));
                indexL = indexR;
            }else{
                indexR++;indexL++;
            }
        }

        if(text.length() > indexR){
            indexR = text.length();
            data.addElement(text.substring(indexL, indexR));
        }

        String splited[] = new String[data.size()];
        for(int i = 0; i < splited.length; i++){
            splited[i] = String.valueOf(data.elementAt(i));
        }

        return splited;
    }

}
